package pers.cz.controller;

import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.ie.InternetExplorerDriver;
import pers.cz.thread.ThreadUtils;

/**
 * @program: PostGirl-panent
 * @description: 测试自动化框架
 * @author: Cheng Zhi
 * @create: 2022-12-24 19:58
 **/
public class TestSelenium {

    public static void main(String[] args) throws InterruptedException {
        System.getProperties().setProperty("webdriver.chrome.driver", "D:\\python\\chromedriver_win32\\chromedriver.exe");
        ChromeOptions chromeOptions = new ChromeOptions();
        ChromeDriver chromeDriver = new ChromeDriver(chromeOptions);
/*        chromeDriver.get("http://sso.fuse.sh.cmcc:1234/#/login");
        ThreadUtils.doSleep(10000L);
        chromeDriver.findElementByXPath("/html/body/div/div[1]/div/div/div/div/div[2]/div/div/div[2]/div/input").sendKeys("SCB0002");
        ThreadUtils.doSleep(5000L);
        chromeDriver.findElementByXPath("/html/body/div/div[1]/div/div/div/div/div[2]/div/div/div[3]/button").click();
        ThreadUtils.doSleep(10000L);
        String lastHandle = null;
        for (String handle : chromeDriver.getWindowHandles()) {
            chromeDriver.switchTo().window(handle);
            System.out.println(chromeDriver.getCurrentUrl());
        }*/

        chromeDriver.get("http://ssoproj.crm.sh.cmcc:5555/?referer=http%3A%2F%2Fsso.fuse.sh.cmcc%3A1234%2F%23%2Fhome#");

        //System.out.println(chromeDriver.getCurrentUrl());
        try {
            String text = chromeDriver.switchTo().alert().getText();
            System.out.println(text);
            chromeDriver.switchTo().alert().accept();
        } catch (Exception e) {

        }

        chromeDriver.findElementByXPath("/html/body/div/div/form/input[2]").sendKeys("SCB0002");
        chromeDriver.findElementByXPath("/html/body/div/div/form/input[3]").sendKeys("123456");
        chromeDriver.findElementByXPath("/html/body/div/div/form/input[4]").sendKeys("ALLPASS");

        chromeDriver.findElementById("Login").sendKeys(Keys.ENTER);
        // 登录成功
        ThreadUtils.doSleep(10000L);
        // 切换窗口
        String lastHandle = null;
        for (String handle : chromeDriver.getWindowHandles()) {
            lastHandle = handle;
        }
        chromeDriver.switchTo().window(lastHandle);
        System.out.println(chromeDriver.getCurrentUrl());

        ThreadUtils.doSleep(50000L);

        // 点击弹框
        try {
            //chromeDriver.findElementByXPath("/html/body/div[3]/div/div[3]/button").click();
            //chromeDriver.findElementByXPath("/html/body/div[2]/div/div[3]/button").click();
            chromeDriver.findElementByXPath("/html/body/div[3]/div/div[3]/button").click();
            ThreadUtils.doSleep(5000L);
        } catch (Exception e) {

        }

        // 输入手机号码：
        chromeDriver.findElementByXPath("/html/body/div[1]/div[1]/div[2]/div/div[2]/div/div/div[1]/div/div/div[1]/div[2]/div[1]/div[1]/div/div[1]/input").sendKeys("18800270336");
        chromeDriver.findElementByXPath("/html/body/div[1]/div[1]/div[2]/div/div[2]/div/div/div[1]/div/div/div[1]/div[2]/div[1]/div[2]/button").click();

        ThreadUtils.doSleep(5000L);

        // 如果获取用户信息异常了
        try {
            chromeDriver.findElementByXPath("/html/body/div[3]/div/div[3]/button").click();
        } catch (Exception e) {
            System.out.println("没有弹窗1");
        }
        // 点击弹窗获取手机号码地市出错
        try {
            chromeDriver.findElementByXPath("/html/body/div[3]/div/div[3]/button").click();
        } catch (Exception e) {
            System.out.println("没有弹窗");
            chromeDriver.switchTo().alert().accept();
            System.out.println("点了一下");
        }
        ThreadUtils.doSleep(50000L);
        // 点击敏感信息鉴权
        try {
            chromeDriver.findElementByXPath("/html/body/div[3]/div/div[3]/button[2]").click();
        } catch (Exception e) {
            System.out.println("没有弹窗");
            chromeDriver.findElementByXPath("/html/body/div[1]/div[1]/div[2]/div/div[2]/div/div/div[1]/div/div/div[1]/div[2]/div[1]/div[2]/button").click();
            ThreadUtils.doSleep(50000L);
            chromeDriver.findElementByXPath("/html/body/div[3]/div/div[3]/button").click();
            ThreadUtils.doSleep(50000L);
            chromeDriver.findElementByXPath("/html/body/div[3]/div/div[3]/button[2]").click();
        }

        ThreadUtils.doSleep(5000L);
        // 点击并输入具体查询理由
        try {
            chromeDriver.findElementByXPath("/html/body/div[4]/div[1]/div[2]/div/div[2]/div[1]/button[2]").click();
            chromeDriver.findElementByXPath("/html/body/div[4]/div[1]/div[2]/div/div[2]/div[3]/div[3]/button").click();
        } catch (Exception e) {
            System.out.println("没有弹窗");
        }

        ThreadUtils.doSleep(10000L);

        chromeDriver.findElementByXPath("/html/body/div[4]/div/div[3]/button[1]").click();
        ThreadUtils.doSleep(10000L);
        chromeDriver.findElementByXPath("/html/body/div[4]/div/div[3]/button[1]").click();
        ThreadUtils.doSleep(10000L);
        chromeDriver.findElementByXPath("/html/body/div[4]/div/div[3]/button[2]").click();
        ThreadUtils.doSleep(10000L);

        chromeDriver.findElementByXPath("/html/body/div[1]/div[1]/div[2]/div/div[2]/div/div/div[1]/div/div/div[1]/div[2]/div[4]/div/div/div/input").sendKeys("帐单查询（H5）");
        ThreadUtils.doSleep(1000L);
        //chromeDriver.findElementByXPath("/html/body/div[7]/div[1]/div[1]/ul/li[1]").click();
        try {
            chromeDriver.findElementByXPath("/html/body/div[6]/div[1]/div[1]/ul/li[1]").click();
        } catch (Exception e) {
            System.out.println("点击下拉框出错");
        }
        // 实名认证窗口
        ThreadUtils.doSleep(1000L);

        //chromeDriver.findElementByXPath("/html/body/div[6]/div/div[3]/button").click();

    }
}
